package com.lyf.scm.admin.domain.service.impl;

import com.lyf.scm.admin.domain.remote.basedata.dto.AreaDTO;
import com.lyf.scm.admin.domain.service.OutService;
import com.lyf.scm.admin.domain.service.PermissionService;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.rome.user.common.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 权限服务
 *
 * @author Martin
 * @date 2020-11-06 15:15
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {

    public static final String DATA_PERMISSION_CODE_AREA = "planArea";
    public static final String DATA_PERMISSION_CODE_STORE = "StorePage";
    public static final String DATA_PERMISSION_CODE_CHANNEL = "kpChannel";
    public static final String DATA_PERMISSION_CODE_LEVEL = "ScmPlanConfirmLevel";

    public static final String DATA_PERMISSION_PREFIX_AREA_ = "scm_plan_data_permission_area_";

    @Resource
    private OutService outService;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<SalesPlanSelectDTO> getPermissionAreaList() {
        log.info("用户信息,{},{}", UserContext.getUserId(), UserContext.getApplicationUser());
        Long userId = UserContext.getUserId();
        if (userId == null || userId == 0) {
            return null;
        }
//        String cacheKey = DATA_PERMISSION_PREFIX_AREA_ + userId;
//        List<SalesPlanSelectDTO> cacheData = (List<SalesPlanSelectDTO>) redisTemplate.opsForValue().get(cacheKey);
//        if (cacheData != null) {
//            return cacheData;
//        }

        Map<String, Set> dataPermissions = UserContext.getDataPermissions();
        log.info("getPermissionAreaList获取到数据权限为：{}",dataPermissions);
        if (dataPermissions == null) {
            return null;
        }
        Set<String> set = dataPermissions.get(DATA_PERMISSION_CODE_AREA);
        Set<String> set2 = dataPermissions.get(DATA_PERMISSION_CODE_STORE);
        Set<String> newSet = new HashSet<>();
        if (set != null && set.size() > 0) {
            newSet.addAll(set);
        }
        if (set2 != null && set2.size() > 0) {
            newSet.addAll(set2);
        }
        if (newSet == null || newSet.size() == 0) {
            return null;
        }
        // 取区域
        List<AreaDTO> areaDTOS = outService.searchAllAreaList();
        Map<String, AreaDTO> areaDTOMap = new HashMap<>();
        for (AreaDTO areaDTO : areaDTOS) {
            areaDTOMap.put(areaDTO.getCode(), areaDTO);
        }
        List<SalesPlanSelectDTO> result = new ArrayList<>();
        for (String code : newSet) {
            AreaDTO areaDTO = areaDTOMap.get(code);
            if (areaDTO != null) {
                SalesPlanSelectDTO salesPlanSelectDTO = new SalesPlanSelectDTO();
                salesPlanSelectDTO.setCode(areaDTO.getCode());
                salesPlanSelectDTO.setLevel(areaDTO.getLevel());
                salesPlanSelectDTO.setName(areaDTO.getName());
                salesPlanSelectDTO.setParentCode(areaDTO.getParentCode());
                result.add(salesPlanSelectDTO);
            }
        }
//        if (result.size() > 0) {
//            redisTemplate.opsForValue().set(cacheKey, result, Duration.ofMinutes(30));
//        }
        return result;
    }

    @Override
    public List<String> getPermissionChannelList() {
        log.info("用户信息,{},{}", UserContext.getUserId(), UserContext.getApplicationUser());
        Map<String, Set> dataPermissions = UserContext.getDataPermissions();
        log.info("getPermissionChannelList获取到数据权限为：{}",dataPermissions);
        if (dataPermissions == null) {
            return null;
        }
        Set<String> set = dataPermissions.get(DATA_PERMISSION_CODE_CHANNEL);
        if (set == null || set.size() == 0) {
            return null;
        }
        return new ArrayList<>(set);
    }

    @Override
    public Integer getPermissionLevel() {
        log.info("用户信息,{},{}", UserContext.getUserId(), UserContext.getApplicationUser());
        Map<String, Set> dataPermissions = UserContext.getDataPermissions();
        log.info("getPermissionLevel获取到数据权限为：{}",dataPermissions);
        if (dataPermissions == null) {
            return null;
        }
        Set<String> set = dataPermissions.get(DATA_PERMISSION_CODE_LEVEL);
        if (set == null || set.size() == 0) {
            return null;
        }
        return set.stream().map(Integer::new).max(Comparator.naturalOrder()).get();
    }

}
